myApp.controller('planListController', function ($scope, $stateParams, ngDialog, $http, $filter, $interval, $uibModal) {
    $scope.info = {};
    $scope.projectList = [];
    $scope.info.isOnline = false;

    $scope.info.modifiAble = true;
    $scope.timer = null;
    if (!isUserCanOperateByRole('plan_admin', 'pm')) {
        $scope.info.modifiAble = false;
    }

    $scope.$on("$destroy", function () {
        if ($scope.timer != null) {
            console.log("清除项目定时器;");
            $interval.cancel($scope.timer);
        }
    });

    $scope.planFilter = function (plans) {
        var list = [];
        var userAccount = getUserAccount();

        if (isUserCanOperateByRole('plan_admin')) {
            return plans;
        }
        angular.forEach(plans, function (plan) {
            if (plan.PM == userAccount) {
                list.push(plan);
            } else {
                var members = plan.members;
                angular.forEach(members, function (member) {
                    if (member.account == userAccount) {
                        list.push(plan);
                        return;
                    }
                });
            }
        });
        return list;
    };

    $scope.planList = function () {
        var url = beetle_plan_url + '/project_plan/getPlanList?isEmergency=1&token_id=' + getTokenId() + '&isShowOnline=' + $scope.info.isOnline;
        if ($scope.info.start_date != undefined && $scope.info.end_date != undefined) {
            url = url + '&start_date=' + $filter('date')($scope.info.start_date, "yyyy-MM-dd") + '&end_date=' + $filter('date')($scope.info.end_date, "yyyy-MM-dd")
        }
        $http.get(url).success(function (data) {
            $scope.projectList = $scope.planFilter(data.data);
            $scope.startTimer();
            $scope.page.index = 1;
        });
    };

    $scope.planList();

    $scope.startTimer = function () {
        if ($scope.timer != null) {
            //校验是否还需要定时，不需要则取消
            $scope.info.isNeedTimer = false;
            for (var i = 0; i < $scope.projectList.length; i++) {
                if ($scope.projectList[i].notCreateProCount != 0) {
                    $scope.info.isNeedTimer = true;
                }
            }
            if (!$scope.info.isNeedTimer) {
                $interval.cancel($scope.timer);
            }
            return;
        }
        angular.forEach($scope.projectList, function (plan) {
            if (plan.notCreateProCount != 0) {
                if ($scope.timer == null) {
                    $scope.timer = $interval(function () {
                        //清空缓存
                        $http.post(beetle_plan_url + '/cache/clearCache').success(function (data) {});
                        $scope.planList()
                    }, 20000);
                }
            }
        })
    };

    $scope.sub = function () {
        if ($scope.info.start_date != undefined || $scope.info.end_date != undefined) {
            if ($scope.info.start_date == undefined) {
                alert('请输入查询开始时间!');
                return false;
            }
            if ($scope.info.end_date == undefined) {
                alert('请输入查询结束时间!');
                return false;
            }

            if ($scope.info.start_date > $scope.info.end_date) {
                alert('开始时间大于结束时间，请重新输入');
                return false;
            }
        }
        $scope.planList();
    };
    $scope.total = $scope.projectList.length;
    $scope.page = {size: 15, index: 1};


    $scope.Project = function (id) {
        window.location.href = "#/menu/plan_ProjectList/" + id;
    };


    $scope.add = function () {
        if ($scope.info.modifiAble) {
            window.location.href = "#/menu/plan_Guide/";
        }
    };

    $scope.del = function (plan_id) {
        if (!$scope.info.modifiAble) {
            return;
        }
        openDialog(ngDialog, $scope, "删除不可恢复，请确认！", true, function (val) {
            if (!val) {
                return;
            }

            openDialog(ngDialog, $scope, "是否要删除项目内工程的svn目录?", true, function (val) {
                doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/delPlan?token_id=' + getTokenId() + '&plan_id=' + plan_id + '&type=2' + '&is_del_svn=' + val, function (data) {
                    if (data.success) {
                        $scope.planList();
                    } else {
                        openDialogAlert(ngDialog, $scope, data.msg);
                    }
                })
            });
        })
    };
});

